package com.jd.controller;

import com.common.model.Result;
import com.jd.model.VCProductInfoCondition;
import com.jd.model.VCProductInfoVO;
import com.jd.service.JDProductInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *  JD商品信息
 */
@RestController
public class JDProductInfoController {

	private static Logger LOG = LoggerFactory.getLogger(JDProductInfoController.class);

	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

	@Autowired
	private JDProductInfoService jdProductInfoService;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping("/common/productinfos")
	public Result<VCProductInfoVO> productinfos(@ModelAttribute VCProductInfoCondition condition) {
		return jdProductInfoService.loadProductInfos(condition);
	}

	@RequestMapping("/common/productinfos/withoutpic")
	public Result<VCProductInfoVO> productinfoswithoutpic(@ModelAttribute VCProductInfoCondition condition) {
		condition.setIsPrimary(0);
		return jdProductInfoService.loadProductInfos(condition);
	}

	// 图片下载
	@RequestMapping("/common/productpic/download")
	public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 二进制数据流
		response.setContentType("application/octet-stream");

		// 附件形式打开
		response.setHeader("Content-Disposition", "attachment; filename=" + new String(("test" +  ".zip").getBytes("GBK"),"ISO-8859-1"));


		jdProductInfoService.downloadPics(response.getOutputStream());


	}

	// 图片上传
	@RequestMapping(value="/common/productpic/upload",method=RequestMethod.POST)
	@ResponseBody
	public String uploadFile(MultipartFile file, HttpServletRequest request) {
		try{
			jdProductInfoService.uploadFile(file.getInputStream());
		}catch(Exception e) {
			e.printStackTrace();
			return "上传失败";
		}
		return "上传成功";
	}



	@RequestMapping("/common/productinfos/publish")
	public Result<VCProductInfoVO> productinfospublish(@ModelAttribute VCProductInfoCondition condition) {
		condition.setSaleState(2);
		condition.setPublishexport("N");
		return jdProductInfoService.loadProductInfos(condition);
	}

	// 上柜申请表
	@RequestMapping("/common/publishexport/download")
	public void publishexport(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 二进制数据流
		response.setContentType("application/octet-stream");

		String fileName = "wysgyp_上架_帷易胜_" + DATE_FORMAT.format(new Date()) + ".xlsx";

		String userAgent = request.getHeader("User-Agent");
		if(StringUtils.contains(userAgent, "Firefox")){//火狐浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
		}else{//IE，google等其他浏览器
			fileName = URLEncoder.encode(fileName,"UTF8");
		}
		response.setHeader("Content-disposition", "attachment;filename=" + fileName);

		jdProductInfoService.publishexport(response.getOutputStream());


	}


	@RequestMapping("/common/productinfos/ep")
	public Result<VCProductInfoVO> productinfosep(@ModelAttribute VCProductInfoCondition condition) {
		condition.setSaleState(2);
		condition.setEpexport("N");
		return jdProductInfoService.loadProductInfos(condition);
	}

	// 入EP池申请表
	@RequestMapping("/common/epexport/download")
	public void epexport(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 二进制数据流
		response.setContentType("application/octet-stream");

		String fileName = "wysgyp_入EP池_帷易胜_" + DATE_FORMAT.format(new Date()) + ".xlsx";

		String userAgent = request.getHeader("User-Agent");
		if(StringUtils.contains(userAgent, "Firefox")){//火狐浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
		}else{//IE，google等其他浏览器
			fileName = URLEncoder.encode(fileName,"UTF8");
		}
		response.setHeader("Content-disposition", "attachment;filename=" + fileName);

		jdProductInfoService.epexport(response.getOutputStream());


	}



}
